﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T07Interface
{
    class Program
    {
        static void Main(string[] args)
        {
            BankCard card1 = new BankCard("张三");
            CreditCard card2 = new CreditCard("李四");
            Ipay[] pays = { card1, card2,
            new BankCard("王五"){Money=150} };
            foreach (Ipay pay in pays)
            {
                if (pay.pay(200))
                {
                    Console.WriteLine("{0}付款成功", pay.GetName());
                }
                else
                {
                    Console.WriteLine("{0}付款失败", pay.GetName());
                }
            }
            Console.ReadKey();
        }
    }

    /// <summary>
    ///  IXXXX形式表示接口    XXXXable也表示接口
    ///  
    /// 付款接口契约
    /// </summary>

    interface Ipay
    {
        bool pay(decimal price);
        string GetName();
    }

    /// <summary>
    /// 借记卡类
    /// </summary>

    class BankCard : Ipay
    {
        //字段
        private string name;
        
        public BankCard(string name)
        {
            this.name = name;
        }

        //属性
        public decimal Money = 1000;

        //生成相关代码：a/t + enter

        public string GetName()
        {
            return name;
        }

        public bool pay(decimal price)
        {
            if(Money>=price)
            {
                Money -= price;
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    /// <summary>
    /// 信用卡类
    /// </summary>

    class CreditCard : Ipay
    {
        private string name;
                
        public CreditCard(string name)
        {
            this.name=name;
        }
        //属性

        public decimal money =1000;

        public string GetName()
        {
            return this.name;
        }

        public bool pay(decimal price)
        {
            if ( money >= price)
            {
                money -= price;
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
